package com.example.rouroudate

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.drawerlayout.widget.DrawerLayout
import com.example.rouroudate.databinding.ActivityMainBinding
import com.example.rouroudate.ui.gallery.GalleryActivity
import com.example.rouroudate.ui.history.HistoryActivity
import com.example.rouroudate.ui.home.HomeFragment
import com.example.rouroudate.ui.slideshow.SlideshowActivity
import com.example.rouroudate.ui.widget.BaseActivity
import com.example.rouroudate.util.DisplayUtils
import com.example.rouroudate.util.StatusBarUtils.getStatusHeight
import com.example.rouroudate.util.StatusBarUtils.initStatus
import com.google.android.material.navigation.NavigationView

class MainActivity : BaseActivity() {

    companion object {
        const val TAG = "MainActivity"
        const val GALLERY_TAG = 1
        const val SLIDES_TAG = 2
    }

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initView()
    }

    private fun handlerDateHistory(view: View) {
        Log.d(TAG, "handlerDateHistory $view")
        val intent = Intent(this, HistoryActivity::class.java)
        startActivity(intent)
    }

    private fun initFragment() {
        val transition = supportFragmentManager.beginTransaction()
        transition.add(R.id.nav_host_fragment_content_main, HomeFragment())
        transition.commit()
    }

    private fun openPage(type: Int) {
        when(type) {
            GALLERY_TAG -> {
                val intent = Intent(this, GalleryActivity::class.java)
                startActivity(intent)
            }
            else -> {
                val intent = Intent(this, SlideshowActivity::class.java)
                startActivity(intent)
            }
        }
    }

    @SuppressLint("UseCompatLoadingForDrawables", "ResourceType")
    private fun initView() {
        val statusBarHeight = getStatusHeight(this)
        val drawerLayout: DrawerLayout = binding.drawerLayout
        val navView: NavigationView = binding.navView.apply {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                itemIconTintList = resources.getColorStateList(R.drawable.nav_item_color, null)
                itemTextColor = resources.getColorStateList(R.drawable.nav_item_color, null)
            }

            setNavigationItemSelectedListener {
                binding.appBarMain.titleBar.titleTv.text = it.title
                when(it.itemId) {
                    R.id.nav_gallery -> openPage(GALLERY_TAG)
                    else -> openPage(SLIDES_TAG)
                }
                true
            }
        }

        binding.appBarMain.titleBar.apply {
            titleTv.setTextColor(ContextCompat.getColor(this@MainActivity, R.color.pink))
            titleTv.text = getString(R.string.menu_home)
            leftView.background = getDrawable(R.drawable.ic_user_profile)
            leftView.setOnClickListener {
                drawerLayout.openDrawer(navView)
            }
            rightView.background = getDrawable(R.drawable.ic_date_history)
            rightView.setOnClickListener(::handlerDateHistory)
        }

        binding.navView.apply {
            setPadding(paddingLeft, statusBarHeight, paddingRight, paddingBottom)
        }

        initFragment()
    }
}